package com.aps.common.utils.converter;

import com.aps.common.model.genetic.Equipment;
import com.aps.common.model.standardAps.StandardApsEquipment;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jsy
 * @description:机器转换器
 * @date 2025/9/23 15:50
 */
public class EquipmentConverter {
    /**
     * @description: 批量转换StandardApsEquipment列表为Equipment列表,globalStartTime是null的时候就是单纯的转换对象
     * @author: jsy
     * @time: 2025/9/23 15:53
     */
    public static List<Equipment> convert(List<StandardApsEquipment> apsEquipments, LocalDateTime globalStartTime) {
        List<Equipment> equipments = new ArrayList<>();
        if (apsEquipments == null || apsEquipments.isEmpty()) {
            return equipments;
        }
        for (StandardApsEquipment apsEquipment : apsEquipments) {
            equipments.add(convertSingle(apsEquipment,globalStartTime));
        }
        return equipments;
    }

    /**
     * @description: 将StandardApsEquipment转换为Equipment,globalStartTime是null的时候就是单纯的转换对象
     * @author: jsy
     * @time: 2025/9/23 15:53
     */
    public static Equipment convertSingle(StandardApsEquipment apsEquipment, LocalDateTime globalStartTime) {
        if (apsEquipment == null) {
            return null;
        }
        Equipment equipment = new Equipment(apsEquipment.getId(),apsEquipment.getSerialNumber(),apsEquipment.getEquipmentType(),globalStartTime);
//        // 映射对应字段
//        equipment.setId(apsEquipment.getId());
//        equipment.setSerialNumber(apsEquipment.getSerialNumber());
//        // 设备类型字段映射（StandardApsEquipment的equipmentType对应Equipment的type）
//        equipment.setType(apsEquipment.getEquipmentType());
//        // StandardApsEquipment无nextAvailableTime字段，globalStartTime可以是null
//        equipment.setNextAvailableTime(globalStartTime);
        return equipment;
    }

    /**
     * @description: 以type为key，相同type的Equipment列表为value的Map
     * @author: jsy
     * @time: 2025/9/23 15:57
     */
    public static Map<String, List<Equipment>> convertToMap(List<Equipment> equipments) {
        // 处理空列表，避免空指针异常
        if (equipments == null) {
            return Collections.emptyMap();
        }

        // 使用toMap转换：key为Order的id，value为Order本身；重复key时保留后者
        // 处理空列表情况，避免空指针异常
        if (equipments == null) {
            return Collections.emptyMap();
        }
        // 按设备类型分组，同一个类型的设备会被放入同一个List中
        return equipments.stream()
                .collect(Collectors.groupingBy(Equipment::getType));
    }
}
